package org.example.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.example.dto.DepartmentDTO;
import org.example.dto.DoctorDTO;
import org.example.dto.OrgInfoDTO;
import org.example.dto.ScheduleDTO;
import org.example.entity.Department;
import org.example.entity.DoctorInfo;
import org.example.entity.OrgInfo;
import org.example.entity.ScheduleInfo;
import org.example.server.mapper.*;
import org.example.server.service.ScheduleService;
import org.example.vo.DoctorsVO;
import org.example.vo.ScheduleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Resource
    private OrgInfoMapper orgInfoMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private ScheduleMapper scheduleMapper;
    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private ScheNumInfoMapper scheNumInfoMapper;


    @Override
    public ScheduleVO getScheduleList(ScheduleDTO scheduleDTO) {




        Department department = departmentMapper.selectByCode(scheduleDTO);

        List<DoctorInfo> doctorInfoList = doctorMapper.selectByDeptCode(scheduleDTO);

        ScheduleVO scheduleVO = new ScheduleVO();

        if (doctorInfoList != null && department != null) {

            List<DoctorsVO> doctorsVOList = new ArrayList<>();


            if (scheduleDTO.getScheDate() == null) {
                //查询全部的排班信息
                scheduleVO.setDeptCode(scheduleDTO.getDeptCode());
                scheduleVO.setDeptName(department.getDepartmentName());

                doctorInfoList.forEach(doctorInfo -> {
                    DoctorsVO doctorsVO = new DoctorsVO();
                    BeanUtils.copyProperties(doctorInfo, doctorsVO);

                    List<ScheduleInfo> scheduleInfoList = scheduleMapper.selectByDoctorCode(doctorInfo.getDoctorCode());

                    if (scheduleInfoList != null) {

                        scheduleInfoList.forEach(scheduleInfo -> {
                            doctorsVO.setSurplus(scheduleInfo.getSurplus());
                            doctorsVO.setScheduleId(scheduleInfo.getScheduleId());
                            doctorsVO.setRegisterFee(scheduleInfo.getRegisterFee());
                        });

                    }
                    doctorsVO.setDeptName(department.getDepartmentName());
                    doctorsVOList.add(doctorsVO);
                });

                scheduleVO.setDoctors(doctorsVOList);

            } else {
                //查询某一天的排班信息

                List<ScheduleInfo> scheduleInfoList = scheduleMapper.selectBy(scheduleDTO.getScheDate());

                scheduleInfoList.forEach(scheduleInfo -> {
                    scheduleVO.setDeptName(department.getDepartmentName());
                    scheduleVO.setDeptCode(scheduleInfo.getDeptCode());

                    DoctorsVO doctorsVO = new DoctorsVO();
                    BeanUtils.copyProperties(scheduleInfo, doctorsVO);
                    DoctorDTO doctorDTO = new DoctorDTO(scheduleInfo.getHospitalNo(), scheduleInfo.getDeptCode(), scheduleInfo.getDoctorCode());
                    DoctorInfo doctorInfo = doctorMapper.selectByCode(doctorDTO);
                    if (doctorInfo != null) {
                        doctorsVO.setAvatarImg(doctorInfo.getAvatarImg());
                        doctorsVO.setTitle(doctorInfo.getTitle());
                        doctorsVO.setSkills(doctorInfo.getSkills());
                    }

                    doctorsVO.setDeptName(department.getDepartmentName());
                    doctorsVOList.add(doctorsVO);
                    scheduleVO.setDoctors(doctorsVOList);


                });

            }

        }
        return scheduleVO;
    }
}



